/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/3/15
 */
import React, {Component} from 'react';
import SimpleTable from "../../../commons/components/SimpleTable";
import StandardDropdown from "../../../commons/components/StandardDropdown";
import ApproveHistory from '../../../commons/components/ApproveHistory';
import {mainTabAction} from 'sei-utils';

class MyOrder extends Component {

  state={
    historyKey:'',
  }

  handleHistory = (record) => {
    this.setState({historyKey:record.businessId})
  }

  setHistoryKey=(id)=>{
    this.setState({historyKey:id})
  }

  handleDetail = (data) =>{
    let uri = data.lookUrl;
    let url = data.webBaseAddressAbsolute.replace(/\/$/g,'')+'/'+uri.replace(/^\//g,'');
    if(url.indexOf('?') === -1){
      url=`${url}?id=${data.businessId}`
    } else {
      url=`${url}&id=${data.businessId}`
    }
    mainTabAction.tabOpen({id:data.businessId,name:'查看表单',featureUrl:url})
  }

  getColumns = () => {
    return [
      {
        title: "操作",
        width: 120,
        dataIndex: "operator",
        render: (text, record, index) => {
          const buttons = [
            <a className={'row-operator-item'} key="detail" onClick={() => this.handleDetail(record)}>查看</a>,
            <a className={'row-operator-item'} key="history" onClick={() => this.handleHistory(record)}>流程历史</a>
          ];
          return (
            <div className={'row-operator'} onClick={(e) => {
              e.stopPropagation()
            }}>
              <StandardDropdown
                operator={buttons}
              />
            </div>
          )
        }
      },
      {
        title: '单号',
        dataIndex: 'businessCode',
        width: 110,
        render: (text, record, index) => {
          if (text) {
            return <span title={text}> {text} </span>;
          }
          return null;
        }
      },
      {
        title: '单据名称',
        dataIndex: 'businessName',
        width: 200,
        render: (text, record, index) => {
          if (text) {
            return <span title={text}> {text} </span>;
          }
          return null;
        }
      },
      {
        title: '说明',
        dataIndex: 'businessModelRemark',
        width: 250,
        render: (text, record, index) => {
          if (text) {
            return <span title={text}>{text}</span>;
          }
          return null;
        }
      },
      {
        title: '任务发起人',
        dataIndex: 'creatorName',
        width: 200,
        render: (text, record, index) => {
          if (record) {
            const res = `${record.creatorName }【${record.creatorAccount}】`;
            return <span title={res}>{res}</span>;
          }
          return null;
        }
      },
      {
        title: '创建时间',
        dataIndex: 'createdDate',
        width: 200,
        render: (text, record, index) => {
          if (text) {
            return <span title={text}> {text} </span>;
          }
          return null;
        }
      }
    ];
  }

  render() {
    const {selectedRows, dataSource, handleRowSelectChange, handlePageChange, visible = false} = this.props;
    return (
      <>
      {visible && <SimpleTable
        rowsSelected={selectedRows}
        onSelectRow={handleRowSelectChange}
        data={dataSource}
        columns={this.getColumns()}
        pageChange={handlePageChange}
      />}
      <ApproveHistory historyKey={this.state.historyKey} setHistoryKey={this.setHistoryKey}/>
      </>
    );
  }
}

export default MyOrder;
